package com.lw.service.impl;

import com.lw.entity.table.TbOperationLogTableDef;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryChain;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.lw.entity.TbOperationLog;
import com.lw.mapper.TbOperationLogMapper;
import com.lw.service.TbOperationLogService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import static com.lw.entity.table.TbOperationLogTableDef.TB_OPERATION_LOG;
import static com.lw.entity.table.TbUserTableDef.TB_USER;

/**
 * 操作日志 服务层实现。
 *
 * @author lw
 * @since 2025年04月30日 16:10:43
 */
@Service
public class TbOperationLogServiceImpl extends ServiceImpl<TbOperationLogMapper, TbOperationLog> implements TbOperationLogService {

    @Override
    public Page<TbOperationLog> pageWithCreatorName(Page page, TbOperationLog operationLog) {
        QueryWrapper queryWrapper = QueryWrapper.create()
                .select(TB_OPERATION_LOG.ALL_COLUMNS, TB_USER.NAME.as("creatorName"))
                .from(TB_OPERATION_LOG)
                .leftJoin(TB_USER).on(TB_OPERATION_LOG.CREATOR.eq(TB_USER.ID));

        QueryCondition condition = QueryCondition.createEmpty();

        //内容
        if (StringUtils.hasText(operationLog.getOperation())) {
            condition.and(TB_OPERATION_LOG.OPERATION.like(operationLog.getOperation()));
        }
        //creator
        if (operationLog.getCreator() != null) {
            condition.and(TB_OPERATION_LOG.CREATOR.eq(operationLog.getCreator()));
        }
        //objId
        if (StringUtils.hasLength(operationLog.getObjId())) {
            condition.and(TB_OPERATION_LOG.OBJ_ID.eq(operationLog.getObjId()));
        }
        queryWrapper.where(condition).orderBy(TB_OPERATION_LOG.ID.desc());
        return page(page, queryWrapper);

    }
}
